package com.xi.controller;

import com.xi.pojo.Log;
import com.xi.pojo.Manager;
import com.xi.pojo.Result;
import com.xi.pojo.User;
import com.xi.service.ManagerService;
import com.xi.service.UserService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping
public class LoginController {
    @Autowired
    private ManagerService managerService;

    @Autowired
    private UserService userService;

    /*
     * 管理员登录
     * */
    @PostMapping("/manager")
    public Result ManagerLogin(@RequestBody Manager manager) {
        log.info("管理员登录 , {}", manager);
        //获取得到登录日志
        Log log = managerService.login(manager);
        //如果获取到的登录日志状态为1则说明登录成功
        if (log.getStatus() == 1) {
            return Result.success();
        }
        return Result.error("用户名或密码错误~");
    }

    /*
    * 用户登录
    * */
    @PostMapping("/user/login")
    public Result UserLogin(@RequestBody User user, HttpSession session) {
        log.info("用户登录 , {}", user);
        // 获取得到登录日志
        Log log = userService.login(user);
        // 如果获取到的登录日志状态为1则说明登录成功
        if (log.getStatus() == 1) {
            session.setAttribute("username", user.getUsername());
            return Result.success();
        }
        return Result.error("用户名或密码错误~");
    }

}
